package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.PostResource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class PostResourceDao extends BaseDao<PostResource> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<PostResource> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<PostResource> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<PostResource> list = run.query(sql, new ResultSetHandler<List<PostResource>>() {
            @Override
            public List<PostResource> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    PostResource postResource = new PostResource();

                    postResource.setId(rs.getInt("id"));
                    postResource.setPath(rs.getString("path"));
                    postResource.setPostId(rs.getInt("post_id"));
                    postResource.setResourceId(rs.getInt("resource_id"));
                    postResource.setSort(rs.getInt("sort"));

                    list.add(postResource);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  PostResource 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public PostResource selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, path, post_id, resource_id, sort from mto_post_resource where id = ?";
        PostResource postResource = run.query(sql,new ResultSetHandler<PostResource>() {
            @Override
            public PostResource handle(ResultSet rs) throws SQLException {
                PostResource postResource = null;
                if(rs.next()) {
                    postResource = new PostResource();

                        postResource.setId(rs.getInt("id"));
                        postResource.setPath(rs.getString("path"));
                        postResource.setPostId(rs.getInt("post_id"));
                        postResource.setResourceId(rs.getInt("resource_id"));
                        postResource.setSort(rs.getInt("sort"));
                }
                return  postResource;
            }
        },params);
        return postResource;
    }

    /**
    *
    * @param postResource 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(PostResource postResource) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_post_resource (  ");
        sql.append("  path, post_id, resource_id, sort ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?,  ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            postResource.getPath(),  
            postResource.getPostId(),  
            postResource.getResourceId(),  
            postResource.getSort()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  postResource
    * @return
    * @throws SQLException
    */
    @Override
    public int update(PostResource postResource) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_post_resource           ");
        sql.append(" SET                    ");
            
            sql.append("   path  = ?,  ");
            sql.append("   post_id  = ?,  ");
            sql.append("   resource_id  = ?,  ");
            sql.append("   sort  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                postResource.getPath(), 
                postResource.getPostId(), 
                postResource.getResourceId(), 
                postResource.getSort(), 
            postResource.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_post_resource where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
